﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyWrite.API.Appicant.Command.Roles;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace B.S.DataBase.MyWrite.API.Appicant.CommandHandler.Roles
{
    /// <summary>
    /// 通过角色ID批量删除多个同样的处理
    /// </summary>
    public class RemoveRoleIdCommandHandler : IRequestHandler<RemoveRoleIdCommand, APIResult<string>>
    {
        private readonly IBaseRepository<UserRole> userroleRepos;

        public RemoveRoleIdCommandHandler(IBaseRepository<UserRole> userroleRepos)
        {
            this.userroleRepos = userroleRepos;
        }
        /// <summary>
        /// 通过角色ID批量删除多个同样的处理
        /// </summary>
        public async Task<APIResult<string>> Handle(RemoveRoleIdCommand request, CancellationToken cancellationToken)
        {
            APIResult<string> result = new APIResult<string>();
            //var obj = request.RoleId.Select(x=> new UserRole { RoleId = x}).ToList();
            var obj = userroleRepos.GetAll().Result.Where(x => x.RoleId == request.RoleId).ToList();
            var res = userroleRepos.DeleteRemove(obj).Result;
            if (res > 0)
            {
                result.Code = APIEnum.删除成功;
                result.Msg = APIEnum.删除成功.ToString();
                result.data = res.ToString();
            }
            return await Task.FromResult(result);
        }
    }
}
